Skip to content

Fix --help/--version when a conftest imports Django models - #1285

Open
alimony wants to merge 3 commits into
pytest-dev:mainfrom
alimony:fix-help-version
Open

Fix --help/--version when a conftest imports Django models#1285
alimony wants to merge 3 commits into
pytest-dev:mainfrom
alimony:fix-help-version

Conversation

@alimony

@alimony alimony commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Problem

Running pytest --help or pytest --version fails to load a conftest.py that imports Django models at import time:

pytest.PytestConfigWarning: could not load initial conftests: .../conftest.py

The underlying cause is AppRegistryNotReady: Apps aren't loaded yet., and it becomes a hard error under filterwarnings = error. Running the tests themselves works fine, because that path sets Django up first.

Cause

pytest_load_initial_conftests returned early for --help/--version – added to keep these options working with an invalid DJANGO_SETTINGS_MODULE (#235) – which also skipped django.setup(). pytest still imports the initial conftests for these options, so any top-level model import then hits AppRegistryNotReady.

Fix

Continue with normal Django initialization for --help/--version so conftests import cleanly. If Django can't be configured or set up (e.g. an invalid DJANGO_SETTINGS_MODULE), the failure is tolerated only for these options, so they keep working regardless of Django's state – preserving #235. A real test run still fails loudly.

Tests

Adds a regression test in tests/test_initialization.py covering both --help and --version. It asserts a positive "conftest imported" marker rather than the absence of a warning, so it also covers --version, where pytest records but never prints the conftest-load failure.

Notes

This overlaps with #1275, which addresses the same issue. Two differences: the error tolerance here catches any setup failure (not only ImportError), so a settings module that imports but is improperly configured also won't break --help; and the regression test guards --version explicitly.

pytest_load_initial_conftests returned early for --help/--version, skipping django.setup(). pytest still imports the initial conftests for these options, so a conftest.py with a top-level Django model import failed with AppRegistryNotReady, surfaced as a "could not load initial conftests" warning (a hard error under filterwarnings = error).

Keep setting Django up for --help/--version so those conftests import cleanly. A broken configuration (e.g. an invalid DJANGO_SETTINGS_MODULE) is tolerated only for these options, so they keep working regardless of Django's state, preserving the behavior from pytest-dev#235; a real test run still fails loudly.

Fixes pytest-dev#1152.

Related to pytest-dev#1275, an earlier fix for the same issue.
@alimony
alimony force-pushed the fix-help-version branch from dfb5736 to 29fd49f Compare July 28, 2026 14:30
@alimony

alimony commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

It seems like all PRs are currently blocked by a Django 6.2 incompatibility. I have a fix for it here: #1286

@kingbuzzman

Copy link
Copy Markdown
Member

Please sync master

@alimony

alimony commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

Please sync master

Done!

Comment thread pytest_django/plugin.py
@alimony

alimony commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

@kingbuzzman

I reworked the try block a little bit. Only two things can actually fail:

  1. dj_settings.DATABASES – the forced settings load (ImportError/ImproperlyConfigured when DSM is missing or blows up on import – the original Make "py.test --help" more friendly if DSM is set, but PYTHONPATH is not #235 case)
  2. _setup_django()django.setup() – settings load fine but INSTALLED_APPS is broken

The rest (header appends, os.environ writes, the stash) can't raise, so I moved the stash above the try. Bonus: no more re-indentation in the diff. Note import configurations.importer is now outside the try – let me know if you want it back in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants